package com.gsbelarus.gedemin.lib.ui.activity.drawer.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gsbelarus.gedemin.lib.R;
import com.gsbelarus.gedemin.lib.Utils;
import com.gsbelarus.gedemin.lib.ui.activity.drawer.adapter.table.DrawerItem;
import com.gsbelarus.gedemin.lib.ui.activity.drawer.adapter.table.DrawerProfile;

import java.util.ArrayList;
import java.util.List;

public class DrawerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<DrawerItem> items;
    private DrawerProfile profile;
    private OnItemClickListener onItemClickListener;
    private boolean isTransparentStatusBar = false;

    private int headerCount = 0;

    public enum ItemType {
        HEADER, MAIN_ITEM, OTHER_ITEM, DIVIDER_ITEM
    }

    public DrawerAdapter(Context context) {
        this.context = context;
        items = new ArrayList<DrawerItem>();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        switch (ItemType.values()[viewType]) {
            case HEADER:
                viewHolder = new ProfileItemVH(LayoutInflater.from(parent.getContext()).inflate(R.layout.drawer_profile, parent, false));
                ViewGroup.LayoutParams params = viewHolder.itemView.getLayoutParams();
                if (isTransparentStatusBar) {
                    params.height = context.getResources().getDimensionPixelSize(R.dimen.profile_layout_height) +
                            Utils.getStatusBarHeight(context);
                    viewHolder.itemView.setPadding(0, Utils.getStatusBarHeight(context), 0, 0);
                } else {
                    params.height = context.getResources().getDimensionPixelSize(R.dimen.profile_layout_height);
                    viewHolder.itemView.setPadding(0, 0, 0, 0);
                }
                viewHolder.itemView.setLayoutParams(params);
                break;
            case MAIN_ITEM:
                viewHolder = new DrawerItemVH(LayoutInflater.from(parent.getContext()).inflate(R.layout.drawer_list_item, parent, false));
                viewHolder.itemView.setBackgroundResource(R.drawable.drawer_list_item_main_background);
                break;
            case OTHER_ITEM:
                viewHolder = new DrawerItemVH(LayoutInflater.from(parent.getContext()).inflate(R.layout.drawer_list_item, parent, false));
                break;
            case DIVIDER_ITEM:
                viewHolder = new DividerItemVH(LayoutInflater.from(parent.getContext()).inflate(R.layout.drawer_list_divider, parent, false));
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (ItemType.values()[getItemViewType(position)]) {
            case HEADER:
                ProfileItemVH profileItemVH = (ProfileItemVH) holder;

                profileItemVH.image.setImageDrawable(profile.getProfileImage());
                profileItemVH.text.setText(profile.getText());
                profileItemVH.summary.setText(profile.getSummary());
                profileItemVH.itemView.setBackgroundDrawable(profile.getBackground());

                profileItemVH.itemView.setOnClickListener(profile.getOnClickProfileImageListener());
                profileItemVH.profileInfoLayout.setOnClickListener(profile.getOnClickProfileInfoListener());
                break;
            case MAIN_ITEM:
                initMainOtherItem((DrawerItemVH) holder, position);
                break;
            case OTHER_ITEM:
                initMainOtherItem((DrawerItemVH) holder, position);
                break;
            case DIVIDER_ITEM:
                break;
        }
    }

    private void initMainOtherItem(DrawerItemVH drawerItemVH, int position) {
        DrawerItem item = items.get(position - headerCount);
        if (item.getImage() != null) {
            drawerItemVH.image.setVisibility(View.VISIBLE);
            drawerItemVH.image.setImageDrawable(item.getImage());
        } else
            drawerItemVH.image.setVisibility(View.GONE);

        if (item.getCounter() > 0) {
            drawerItemVH.counter.setVisibility(View.VISIBLE);
            drawerItemVH.counter.setText(String.valueOf(item.getCounter()));
        } else
            drawerItemVH.counter.setVisibility(View.GONE);

        if (item.getText() != null)
            drawerItemVH.text.setText(item.getText());
        else
            drawerItemVH.text.setText("");
    }

    @Override
    public int getItemCount() {
        return items.size() + headerCount;
    }

    @Override
    public int getItemViewType(int position) {
        return (headerCount != 0 && position == 0) ? ItemType.HEADER.ordinal() : items.get(position - headerCount).getType().ordinal();
    }

    public void setProfile(DrawerProfile profile) {
        if (profile == null) {
            if (headerCount == 0) return;
            this.profile = null;
            headerCount = 0;
            notifyItemRemoved(0);

        } else {
            this.profile = profile;
            headerCount = 1;
            notifyItemInserted(0);
        }
    }

    public void addItem(DrawerItem item) {
        items.add(item);
        notifyItemInserted(items.size() - 1 + headerCount);
    }

    public void addItems(List<DrawerItem> items) {
        this.items.addAll(items.size(), items);
        notifyItemRangeInserted(items.size() - 1 + headerCount, items.size());
    }

    public void removeItem(DrawerItem item) {
        if (items.indexOf(item) < 0) return;
        int position = items.indexOf(item) + headerCount;
        items.remove(item);
        notifyItemRemoved(position);
    }

    public void clearAll() {
        items.clear();
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public DrawerProfile getProfile() {
        return profile;
    }

    public boolean isTransparentStatusBar() {
        return isTransparentStatusBar;
    }

    public void setTransparentStatusBar(boolean isTransparentStatusBar) {
        this.isTransparentStatusBar = isTransparentStatusBar;
    }

    public void notifyChangedProfile() {
        if (headerCount > 0)
            notifyItemChanged(0);
    }

    public void notifyChangedItem(DrawerItem item) {
        if (items.indexOf(item) < 0) return;
        notifyItemChanged(items.indexOf(item) + headerCount);
    }

    /**
     * @param id id элемента
     * @return элемент по id
     */
    public DrawerItem getItemById(long id) {
        for (DrawerItem item : items)
            if (item.getId() == id) return item;
        return null;
    }

    public int getPositionItem(DrawerItem item) {
        if (item != null && items.contains(item))
            return items.indexOf(item) + headerCount;
        return -1;
    }

    public List<DrawerItem> getItems() {
        return items;
    }

    public int getHeaderCount() {
        return headerCount;
    }

    public boolean containsItem(DrawerItem item) {
        return items.contains(item);
    }

    public interface OnItemClickListener {
        void onItemClick(DrawerItem item, int position);
    }

    public class DrawerItemVH extends RecyclerView.ViewHolder implements View.OnClickListener {

        public ImageView image;
        public TextView text;
        public TextView counter;

        DrawerItemVH(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.drawer_list_item_image);
            text = (TextView) itemView.findViewById(R.id.drawer_list_item_text);
            counter = (TextView) itemView.findViewById(R.id.drawer_list_item_counter);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            DrawerItem item = items.get(getAdapterPosition() - headerCount);
            if (item.getType() == ItemType.MAIN_ITEM || item.getType() == ItemType.OTHER_ITEM)
                if (onItemClickListener != null)
                    onItemClickListener.onItemClick(item, getAdapterPosition());
        }
    }

    public class ProfileItemVH extends RecyclerView.ViewHolder {

        public ImageView image;
        public TextView text;
        public TextView summary;
        public LinearLayout profileInfoLayout;

        ProfileItemVH(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.drawer_profile_image);
            text = (TextView) itemView.findViewById(R.id.drawer_profile_text);
            summary = (TextView) itemView.findViewById(R.id.drawer_profile_summary);
            profileInfoLayout = (LinearLayout) itemView.findViewById(R.id.drawer_profile_info_layout);

        }
    }

    public class DividerItemVH extends RecyclerView.ViewHolder {

        DividerItemVH(View itemView) {
            super(itemView);
        }
    }
}
